#ifdef MEM_DEBUG

#include <assert.h>
#include <fstream>
#include "Memory.h"
#include "AllocationManager.h"
#include "File.h"

namespace Engine
{
	AllocationManager::AllocationManager() : m_sizeOfAllocs(0), 
		m_maxSizeOfAllocs(0), m_maxHeapSize(0){}

	void AllocationManager::AddAllocation(const Allocation& a)
	{
		m_map[a.GetAddress()] = a;
		m_sizeOfAllocs += a.GetSize();

		if(m_sizeOfAllocs > m_maxSizeOfAllocs)
		{
			if(m_maxHeapSize > 0)
			{
				assert(m_maxSizeOfAllocs < m_sizeOfAllocs);
			}

			m_maxSizeOfAllocs = m_sizeOfAllocs;
		}
	}

	void AllocationManager::RemoveAllocation(void* address)
	{
		auto it = m_map.find(address);

		if(it != m_map.end())
		{
			m_sizeOfAllocs -= it ->second.GetSize();
			m_map.erase(it);
		}
	}

	void AllocationManager::Report(const std::string& s)
	{
		std::ofstream file(s);

		if(file.is_open())
		{
			for(auto it = m_map.begin(); it != m_map.end(); ++it)
			{
				file << it ->second << std::endl;
			}

			file << "Total ammount of bytes leaked:" << "\t" << m_sizeOfAllocs;
			file.close();
		}
	}

	AllocationManager::~AllocationManager()
	{
		IncrementDepth();
		//to prevent recursive deletes when map goes away
		Report();
	}
}

#endif
